package com.qyer.log;

import static com.qyer.search.Constants.SEPARATOR2;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.qyer.search.model.SearchParameter;
import com.qyer.search.model.SearchParameterOrigin;
import com.qyer.search.report.TimeCost;
import com.qyer.search.utils.JsonOperator;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * User: Z J Wu Date: 2015/08/20 Time: 00:00 Package: com.qyer.log
 */
public class TimeCostLogger {

  private static final Logger LOGGER = LoggerFactory.getLogger(TimeCostLogger.class);

  public static void logTimeCost(SearchParameter searchParameter, TimeCost timeCost) throws
    JsonProcessingException {
    if (timeCost == null) {
      return;
    }
    StringBuilder sb = new StringBuilder();
    sb.append(searchParameter.getRequestId());
    sb.append(SEPARATOR2);
    sb.append(searchParameter.getContext());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getWordETLTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getPlanTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getSearchTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getTransformResultTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getSortResultTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getRequestTime());
    sb.append(SEPARATOR2);
    Map<String, Long> otherTimeMap = timeCost.getOtherTimeMap();
    if (MapUtils.isNotEmpty(otherTimeMap)) {
      sb.append(JsonOperator.getInstance().toJson(otherTimeMap));
    }
    LOGGER.info(sb.toString());
  }

  public static void logTimeCost(SearchParameterOrigin searchParameter, TimeCost timeCost) throws
    JsonProcessingException {
    if (timeCost == null) {
      return;
    }
    StringBuilder sb = new StringBuilder();
    sb.append(searchParameter.getRequestId());
    sb.append(SEPARATOR2);
    sb.append(searchParameter.getContext());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getWordETLTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getPlanTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getSearchTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getTransformResultTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getSortResultTime());
    sb.append(SEPARATOR2);
    sb.append(timeCost.getRequestTime());
    sb.append(SEPARATOR2);
    Map<String, Long> otherTimeMap = timeCost.getOtherTimeMap();
    if (MapUtils.isNotEmpty(otherTimeMap)) {
      sb.append(JsonOperator.getInstance().toJson(otherTimeMap));
    }
    LOGGER.info(sb.toString());
  }
}
